package com.project.restaurant.user.controller;

import com.project.restaurant.common.api.ResponseResult;
import com.project.restaurant.common.api.ServiceReturn;
import com.project.restaurant.common.api.ServiceStateCode;
import com.project.restaurant.pojo.User;
import com.project.restaurant.user.dto.RegisterInfo;
import com.project.restaurant.user.service.RegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

@Api(value = "注册接口",tags = "注册模块")
@RestController
@RequestMapping("/user/register")
public class RegisterController {

    @Resource
    private RegisterService registerService;

    @ApiOperation(value = "校验注册参数",notes = "返回值：true代表注册信息符合规范；false代表注册信息不符合规范")
    @ApiResponse(code = 200, message = "操作成功", response = Boolean.class)
    @PostMapping(value = "/info/check")
    public ResponseResult<Boolean> checkRegisterInfo(@ApiParam(value = "注册信息", required = true) @RequestBody @Validated RegisterInfo registerInfo){
        boolean result = registerService.checkRegisterInfo(registerInfo);
        return ResponseResult.success(result);
    }

    @ApiOperation(value = "发送注册邮箱验证码",notes = "返回值：true代表发送成功；false代表发送失败")
    @ApiResponse(code = 200, message = "操作成功", response = Boolean.class)
    @GetMapping(value = "/obtain/email/authCode/{email}")
    public ResponseResult<Boolean> sendRegisterAuthCodeToEmail(@ApiParam(value = "目标邮箱", required = true) @PathVariable("email") String email){
        boolean result = registerService.sendRegisterAuthCodeToEmail(email);
        return ResponseResult.success(result);
    }

    @ApiOperation(value = "注册账户",notes = "")
    @ApiResponse(code = 200, message = "操作成功", response = Boolean.class)
    @PostMapping(value = "")
    public ResponseResult<Boolean> register(@ApiParam(value = "注册信息", required = true) @RequestBody @Validated RegisterInfo registerInfo) {
        //进行参数校验
        registerService.checkRegisterInfo(registerInfo);

        boolean result = registerService.register(registerInfo);
        return ResponseResult.success(result);
    }

}
